package third_part

import cn.jiguang.common.ClientConfig
import cn.jiguang.common.resp.APIConnectionException
import cn.jiguang.common.resp.APIRequestException
import cn.jpush.api.JPushClient
import cn.jpush.api.push.PushResult
import cn.jpush.api.push.model.Platform
import cn.jpush.api.push.model.PushPayload
import cn.jpush.api.push.model.audience.Audience
import cn.jpush.api.push.model.notification.Notification

object JpushUtil {
    private fun buildPushObjectAlert(msg: String): PushPayload {
        return PushPayload.newBuilder()
            .setPlatform(Platform.all())
            .setAudience(Audience.registrationId("1104a897925b026797c"))
            .setNotification(Notification.alert(msg))
            .build();
    }

    fun pushMessage(msg: String) {
        val jpushClient =
            JPushClient("6f454a3edda43c34a3b9a89e", "c1a8cf233f549b0655f40b4a", null, ClientConfig.getInstance())

        // For push, all you need do is to build PushPayload object.
        val payload: PushPayload = buildPushObjectAlert(msg)

        try {
            val result: PushResult = jpushClient.sendPush(payload)
            println("Got result - $result")
        } catch (e: APIConnectionException) {
            // Connection error, should retry later
            println("Connection error, should retry later=>$e")
        } catch (e: APIRequestException) {
            // Should review the error, and fix the request
            println("Should review the error, and fix the request=>$e")
            println("HTTP Status: " + e.status)
            println("Error Code: " + e.errorCode)
            println("Error Message: " + e.errorMessage)
        }
    }
}
